package com.ruoyi.quartz.task;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.domain.Emply;
import com.ruoyi.common.domain.Holidays;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.domain.CsceceeEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("kpTask")
public class KqPersonTask {

    @Autowired
    private ISysJobService sysJobService;

    @Autowired
    private com.ruoyi.system.service.ICsceceeEmployeeService ICsceceeEmployeeService;

    public static int ldflag = 1;

    public static int zyflag = 2;
    public Map<String, Integer> IdFlagMap = new HashMap<>();


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String startDate, String endDate){
        run(startDate, endDate);
    }

    public void autoRun(){
        String currentDate = DateUtils.getDate();
        run(currentDate, currentDate);
    }

    public void run(String startDate, String endDate){

       // List<Holidays> holidaysList = getDate(startDate, endDate);
        try {
            List<Emply> emplyList = sysJobService.getEmplyList();
            CsceceeEmployee query = new CsceceeEmployee();
            query.setLdFlag(ldflag);
            List<CsceceeEmployee> AllData =ICsceceeEmployeeService.selectCsceceeEmployeeList(query);
            if(!CollectionUtils.isEmpty(AllData)) {
               IdFlagMap =  AllData.stream()
                        .collect(Collectors.toMap(CsceceeEmployee::getEmployeeId, CsceceeEmployee::getLdFlag));
            }

            ICsceceeEmployeeService.clearData();

            List<CsceceeEmployee> list = new ArrayList<>();
            emplyList.forEach(data -> {
                CsceceeEmployee ce = new CsceceeEmployee();
                ce.setDepartId(Convert.toLong(data.getDepartId()));
                ce.setDepartName(data.getDepartName());
                ce.setEmployeeId(data.getEmployeeId());
                ce.setEmployeeName(data.getEmployeeName());
                if(IdFlagMap.containsKey(data.getEmployeeId())) {
                    ce.setLdFlag(ldflag);
                }else {
                    ce.setLdFlag(zyflag);
                }
                list.add(ce);
            });
            ICsceceeEmployeeService.insertBatchCsceceeEmployee(list);
//            list.forEach(data -> {
//                ICsceceeEmployeeService.insertCsceceeEmployee(data);
//            });
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public List<Holidays> getDate(String startDate, String endDate){
        return  sysJobService.getHolidaysByDate(startDate, endDate);
    }

    public void ryNoParams(){
        System.out.println("....");
    }

    public void genHolidays(String params)
    {
        sysJobService.genHolidays(params);
    }
}
